<template>
  <el-row :gutter="20">
    <el-col :span="16" :offset="4">
      <div style="margin-top: 100px; padding: 40px; border: 1px solid #f8f8f8; border-radius: 8px; box-shadow: 0 0 8px #8c939d;">
        <h2 style="text-align:center;">登陆</h2>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password" type="password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">登陆</el-button>
            <el-button @click="doClick">注册</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-col>
  </el-row>
</template>

<script>
import axios from "axios";

export default {
  name: "Login",
  data() {
    return {
      ruleForm: {
        username: '',
        password: '',
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let vm = this;
          axios.get("/user/login?type=user&username=" + vm.ruleForm.username + "&password=" + vm.ruleForm.password).then(res => {
            if (res.data != "") {
              vm.$message.success('登录成功');
              vm.$store.dispatch("login", res.data);
              vm.$router.push('/');
            } else {
              vm.$message.error('账号或密码不正确');
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    doClick() {
      this.$router.push("/register");
    }
  }
}
</script>

<style scoped>

</style>